package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbItemParamMapper;
import com.usian.pojo.TbItemParam;
import com.usian.pojo.TbItemParamExample;
import com.usian.redis.RedisClient;
import com.usian.service.ItemParamService;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Service
public class ItemParamServiceImpl implements ItemParamService {

    @Autowired
    private TbItemParamMapper tbItemParamMapper;

    @Value("${PROTAL_CATRESULT_KEY}")
    private String PROTAL_CATRESULT_KEY;


    @Autowired
    RedisClient redisClient;

    @Override
    public TbItemParam selectItemParamByItemCatId(Long itemCatId) {
        TbItemParamExample example = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andItemCatIdEqualTo(itemCatId);
        List<TbItemParam> itemParams = tbItemParamMapper.selectByExampleWithBLOBs(example);
        if (itemParams != null && itemParams.size() > 0) {
            return itemParams.get(0);
        }
        return null;
    }

    @Override
    public PageResult selectItemParamAll(@RequestParam Integer pages,
                                         @RequestParam Integer rows) {
        PageHelper.startPage(pages, rows);
        TbItemParamExample example = new TbItemParamExample();
        example.setOrderByClause("updated desc");
        List<TbItemParam> paramList = tbItemParamMapper.selectByExampleWithBLOBs(example);
        PageInfo<TbItemParam> info = new PageInfo<>(paramList);
        PageResult pageResult = new PageResult();
        pageResult.setResult(info.getList());
        pageResult.setTotalPage(info.getPages());
        pageResult.setPageIndex(info.getPageNum());
        return pageResult;
    }

    @Override
    public Integer insertItemParam(Long itemCatId, String paramData) {

        //查询该类是否有规格模板
        TbItemParamExample example = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andItemCatIdEqualTo(itemCatId);
        List<TbItemParam> list = tbItemParamMapper.selectByExample(example);
        //如果有不能添加
        if (list.size() > 0){
            return 0;
        }

        //如果没有可以添加
        TbItemParam param = new TbItemParam();
        param.setCreated(new Date());
        param.setItemCatId(itemCatId);
        param.setUpdated(new Date());
        param.setParamData(paramData);
        redisClient.del(PROTAL_CATRESULT_KEY);
        return tbItemParamMapper.insertSelective(param);
    }

    @Override
    public Integer deleteItemParamById(Long id) {
        redisClient.del(PROTAL_CATRESULT_KEY);
        return tbItemParamMapper.deleteByPrimaryKey(id);
    }
}